#ifndef _RECONSTRUCT_CMAINWINDOW_H_
#define _RECONSTRUCT_CMAINWINDOW_H_

#include <buola/gui/containers/cmosaiccontainer.h>

#include <buola/scene.h>
#include <buola/scene/csceneview.h>
#include <buola/scene/geodes/cpointcloud.h>
#include <buola/widgets/cimagebox.h>
#include <buola/cv/io/ckinect.h>
#include <buola/cv/features/cdetector.h>
#include <buola/cv/trees/cflanntree.h>

namespace buola { namespace reconstructor {

class CMainWindow : public gui::CMosaicContainer
{
public:
    struct SKeyFrame
    {
        cvi::CFeatureSet mFeatures;
        img::CImage_rgb8 mRGB;
        img::CImage_xyz32f mXYZ;
        mat::CMat34d mTrans;
        cvi::CCloud_rgb8 mDownSampled;
        cvi::CFlannTree<double> mKDTree;
    };
    
    CMainWindow();
    virtual ~CMainWindow();

private:
    void StartCamera();
    void StartRec();
    void StartLoad();

    void SetupScene();
    void SetupViews();
    
    void OnCapture(const img::CImage_rgb8 &pRGB,const img::CImage_gray32f &pDepth);
    void OnTimer();
    void RecThread();
    void LoadThread();

    void ProcessCloud(img::CImage_rgb8 &&pRGB,img::CImage_gray32f &&pDepth);
    
private:
    gui::PToolBar mToolBar;
    
    img::CImage_rgb8 mLastRGB;
    img::CImage_gray32f mLastDepth;

    scene::PScene mScene;
    scene::PSceneView mSceneView;
    scene::PPerspectiveCamera mCamera;
    
    std::unique_ptr<cvi::CKinect> mKinect;
    mutex mCaptureMutex;
    mutex mPendingMutex;
    std::vector<std::unique_ptr<SKeyFrame>> mKeyFrames;
    std::vector<SKeyFrame*> mPendingKeyFrames;
    
    cvi::CSURFDetector mSURF;
    
    CTimer mTimer;
};

/*namespace reconstructor*/ } /*namespace buola*/ }

#endif /* CSERVOWINDOW_H_ */
